package orz.oxlab;


import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import android.widget.TextView;

public class TeamMember extends Activity {

	private static final String LOG_TAG = "Hello0xlab";

	private WebView mWebView;
	private ImageButton dialButton;
	private ImageButton mailButton;
	private ImageButton blogButton;
	private Handler mHandler = new Handler();

	private class HelloWebViewClient extends WebViewClient {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);
			return true;
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.team);
		final String name;
		final String blogURL;
		final String dialNumber;
		final String emailAddress;
		
		TextView dialText = (TextView) findViewById(R.id.dial_text);
		TextView emailText = (TextView) findViewById(R.id.email_text);
		TextView blogText = (TextView) findViewById(R.id.blog_text);
		
		int diff = getIntent().getIntExtra(Intent.EXTRA_TEXT,0);
		Log.d(LOG_TAG, "clicked on " + diff);
        switch(diff){
        
        case R.id.image_erin:
        	name = getString(R.string.erin);
    		blogURL = getString(R.string.erin_blog);
    		dialNumber = getString(R.string.erin_dial);
    		emailAddress = getString(R.string.erin_email);
    		dialText.setText(R.string.erin_dial);
    		emailText.setText(R.string.erin_email);
    		blogText.setText(R.string.erin_blog);
        	break;
        	
        case R.id.image_jeremy:
        	name = getString(R.string.jeremy);
    		blogURL = getString(R.string.jeremy_blog);
    		dialNumber = getString(R.string.jeremy_dial);
    		emailAddress = getString(R.string.jeremy_email);
    		dialText.setText(R.string.jeremy_dial);
    		emailText.setText(R.string.jeremy_email);
    		blogText.setText(R.string.jeremy_blog);
        	break;
        	
        case R.id.image_john:
        	name = getString(R.string.john);
    		blogURL = getString(R.string.john_blog);
    		dialNumber = getString(R.string.john_dial);
    		emailAddress = getString(R.string.john_email);
    		dialText.setText(R.string.john_dial);
    		emailText.setText(R.string.john_email);
    		blogText.setText(R.string.john_blog);
        	break;
        	
        case R.id.image_jserv:
        	name = getString(R.string.jserv);
    		blogURL = getString(R.string.jserv_blog);
    		dialNumber = getString(R.string.jserv_dial);
    		emailAddress = getString(R.string.jserv_email);
    		dialText.setText(R.string.jserv_dial);
    		emailText.setText(R.string.jserv_email);
    		blogText.setText(R.string.jserv_blog);
        	break;
        	
        case R.id.image_julian:
        	name = getString(R.string.julian);
    		blogURL = getString(R.string.julian_blog);
    		dialNumber = getString(R.string.julian_dial);
    		emailAddress = getString(R.string.julian_email);
    		dialText.setText(R.string.julian_dial);
    		emailText.setText(R.string.julian_email);
    		blogText.setText(R.string.julian_blog);
        	break;	
        	
        case R.id.image_matt:
        	name = getString(R.string.matt);
    		blogURL = getString(R.string.matt_blog);
    		dialNumber = getString(R.string.matt_dial);
    		emailAddress = getString(R.string.matt_email);
    		dialText.setText(R.string.matt_dial);
    		emailText.setText(R.string.matt_email);
    		blogText.setText(R.string.matt_blog);
        	break;	
        	
        case R.id.image_olv:
        	name = getString(R.string.olv);
    		blogURL = getString(R.string.olv_blog);
    		dialNumber = getString(R.string.olv_dial);
    		emailAddress = getString(R.string.olv_email);
    		dialText.setText(R.string.olv_dial);
    		emailText.setText(R.string.olv_email);
    		blogText.setText(R.string.olv_blog);
        	break;	
        	
        case R.id.image_thinker:
        	name = getString(R.string.thinker);
    		blogURL = getString(R.string.thinker_blog);
    		dialNumber = getString(R.string.thinker_dial);
    		emailAddress = getString(R.string.thinker_email);
    		dialText.setText(R.string.thinker_dial);
    		emailText.setText(R.string.thinker_email);
    		blogText.setText(R.string.thinker_blog);
        	break;
        	
        case R.id.image_tick:
        	name = getString(R.string.tick);
    		blogURL = getString(R.string.tick_blog);
    		dialNumber = getString(R.string.tick_dial);
    		emailAddress = getString(R.string.tick_email);
    		dialText.setText(R.string.tick_dial);
    		emailText.setText(R.string.tick_email);
    		blogText.setText(R.string.tick_blog);
        	break;	
        	
        default:
        	name = getString(R.string.matt);
			blogURL = getString(R.string.matt_blog);
			dialNumber = getString(R.string.matt_dial);
			emailAddress = getString(R.string.matt_email);
    		dialText.setText(R.string.matt_dial);
    		emailText.setText(R.string.matt_email);
    		blogText.setText(R.string.matt_blog);
        	break;
        }
        
        mWebView = (WebView) findViewById(R.id.webview);
        
        dialButton = (ImageButton) findViewById(R.id.dial_button);
        dialButton.setOnClickListener(new OnClickListener() { 
           public void onClick(View view) {
        	   dailContact(dialNumber);
           }
        });
        
        mailButton = (ImageButton) findViewById(R.id.email_button);
        mailButton.setOnClickListener(new OnClickListener() { 
           public void onClick(View view) {
        	   sendEmail(emailAddress);
           }
        });
        
        blogButton = (ImageButton) findViewById(R.id.blog_button);
        blogButton.setOnClickListener(new OnClickListener() { 
           public void onClick(View view) {
        	   mWebView.loadUrl(blogURL);
           }
        });

        WebSettings webSettings = mWebView.getSettings();
        mWebView.setWebViewClient(new HelloWebViewClient());
        mWebView.setHorizontalScrollBarEnabled(false);
        mWebView.setVerticalScrollBarEnabled(false);
        webSettings.setSavePassword(false);
        webSettings.setSaveFormData(false);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setSupportZoom(false);
        mWebView.setWebChromeClient(new MyWebChromeClient());
        mWebView.addJavascriptInterface(new DemoJavaScriptInterface(), "demo");

        String fileTemplate = "file:///android_asset/";
        if (name != null) {
        	fileTemplate = fileTemplate + name + ".html";
        }
        mWebView.loadUrl(fileTemplate);
	}
	
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
        	mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    
    final class DemoJavaScriptInterface {

        DemoJavaScriptInterface() {
        }

        /**
         * This is not called on the UI thread. Post a runnable to invoke
         * loadUrl on the UI thread.
         */
        public void clickOnAndroid() {
            mHandler.post(new Runnable() {
                public void run() {
                    mWebView.loadUrl("javascript:wave()");
                }
            });

        }
    }
    
    private void sendEmail(String email) {
    	String mailto = "mailto:";
    	mailto = mailto + email;
        Uri uri = Uri.parse(mailto.toString());	
        Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
        startActivity(intent);

	}
    
    private void dailContact(String number) {
       String tel = "tel:";
       tel = tel + number;
       Uri uri = Uri.parse(tel.toString());	
       Intent intent = new Intent(Intent.ACTION_DIAL, uri);
       startActivity(intent);
    }
    /**
     * Provides a hook for calling "alert" from javascript. Useful for
     * debugging your javascript.
     */
    final class MyWebChromeClient extends WebChromeClient {
        @Override
        public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
            Log.d(LOG_TAG, message);
            result.confirm();
            return true;
        }
    }
}
